package com.lucy.management.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;

public class MD5Util {
	
	public static String md5(String src) {
		return DigestUtils.md5Hex(src);
	}
	
	private static final String salt = "1a2b3c4d";


	/**
	 * @return 盐值
	 */
	public static String getSalt() {
		String salt = "1234567890abcdefghijklmnopqrstuvwxyz";
		char[] chars = salt.toCharArray();
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < 6; i++) {
			int v = (int) (Math.random() * salt.length());
			for (int j = 0; j < chars.length; j++) {
				if (v == j) {
					stringBuilder.append(chars[j]);
				}
			}
		}
		return stringBuilder.toString();
	}


	/**
	 * 一次md5
	 * @param formPass：密码
	 * @param salt：盐值
	 * @return MD5字符串
	 */
	public static String formPassToDBPass(String formPass, String salt) {
		Random random = new Random();
		random.nextInt(10);
		String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(5) + salt.charAt(4);
		return md5(str);
	}

	/**
	 * 两次md5
	 * @param inputPass：密码
	 * @param saltDB：盐值
	 * @return MD5
	 */
	public static String inputPassToDbPass(String inputPass, String saltDB) {
		String formPass = formPassToDBPass(inputPass,salt);
		String dbPass = formPassToDBPass(formPass, saltDB);
		return dbPass;
	}

	
}
